package com.shanpin.shebao.admin.module.batchcard;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.shanpin.shebao.admin.db.AdminDbRepository;
import com.shanpin.shebao.admin.module.batchcard.model.TmpCard;
import com.shanpin.shebao.utility.Criteria;
import com.shanpin.shebao.utility.PageModel;

@Repository
public class TmpCardDao {

	@Autowired
	private AdminDbRepository admin;
	
	public PageModel list(PageModel pageModel, Criteria criteria) {
		String select = "  SELECT card.* ";
		String from = "      FROM t_tmp_card card ";
		String where = criteria.getSqlString();
		String order = "    ORDER BY card.create_time DESC ";
		
		StringBuilder querySql = new StringBuilder();
		querySql.append(select).append(from).append(where).append(order);
		StringBuilder totalSql = new StringBuilder();
		totalSql.append("SELECT COUNT(1) ").append(from).append(where);

		//分页处理
		int pageNo = pageModel.getPageNo();
		int pageSize = pageModel.getPageSize();
		int start = (pageNo-1)*pageSize;
		String sql = pageModel.getMySQLPageSQL(querySql.toString(), start, pageSize);
		
		List<TmpCard> cardList = admin.query(sql, TmpCard.mapper, criteria.getValueList().toArray());
		int totalCount = admin.queryInt(totalSql.toString(), criteria.getValueList().toArray());
		
		pageModel.setPageNo(pageNo);
		pageModel.setPageSize(pageSize);
		pageModel.setRecordList(cardList);
		pageModel.setTotalRecord(totalCount);
		
		return pageModel;
	}
	
	public TmpCard get(int id) {
		String sql = " SElECT * FROM t_tmp_card WHERE id = ? ";
		return admin.queryT(sql, TmpCard.mapper, id);
	}

	public List<TmpCard> getByIds(List<Integer> ids) {
		HashMap<String, Object> map = new HashMap<>();
		String sql = " SElECT * FROM t_tmp_card WHERE id IN (:ids) ";
		map.put("ids",ids);
		return admin.queryByNamed(sql, map,TmpCard.mapper);
	}
	
	public boolean update(int id, int status, String memo) {
		String sql = " UPDATE t_tmp_card SET status=?, memo=? WHERE id = ? ";
		return admin.update(sql, status, memo, id);
	}

	public int batchUpdate(List<Integer> ids, int status, String memo) {
		String sql = " UPDATE t_tmp_card SET status=:status, memo=:memo WHERE id IN (:ids) ";
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("status",status);
		paramMap.put("memo",memo);
		paramMap.put("ids",ids);
		return admin.update(sql, paramMap);
	}

	
	public int batchAdd(String batch_no, List<TmpCard> cards) {
		Date curDate = new Date();
		List<Object[]> list = new ArrayList<>();
		for(TmpCard card : cards) {
			Object[] param = new Object[]{batch_no, card.getId_card(), 
					card.getShebao_card(), card.getFund_card(), curDate};
			list.add(param);
		}
		String sql = "INSERT INTO t_tmp_card (batch_no, id_card, shebao_card, fund_card, create_time) values (?, ?, ?, ?, ?)";
		return admin.updateBatch(sql, list);
	}
	
	public int batchDelete(List<Integer> idList) {
		List<Object[]> list = new ArrayList<>(idList.size());
		for(Integer id : idList) {
			list.add(new Object[]{id});
		}
		String sql = "DELETE FROM t_tmp_card WHERE id = ?";
		return admin.updateBatch(sql, list);
	}
}
